﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.Consts;

namespace BySoft.Server.DataAccess
{
    public partial class SysParameterDA
    {
        public static int Update(SysParameterEntity entity)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                 #region SQL Server
                    string strSQL = "update " + entity.TableSchema.TableName + " set ";
                    strSQL += SysParameterTable.C_PARAMETERNAME + "=@" + SysParameterTable.C_PARAMETERNAME;
                    strSQL += ", " + SysParameterTable.C_MODIFYDATE + "=@" + SysParameterTable.C_MODIFYDATE;
                    strSQL += ", " + SysParameterTable.C_MODIFYUSERID + "=@" + SysParameterTable.C_MODIFYUSERID;
                    strSQL += ", " + SysParameterTable.C_ORDERNUMBER + "=@" + SysParameterTable.C_ORDERNUMBER;
                    strSQL += ", " + SysParameterTable.C_DESCRIPTION + "=@" + SysParameterTable.C_DESCRIPTION;
                    strSQL += "," + SysParameterTable.C_PARATMETERVALUE + "=@" + SysParameterTable.C_PARATMETERVALUE;
                    strSQL += "," + SysParameterTable.C_STATUS + "=@" + SysParameterTable.C_STATUS;
                    strSQL += " where " + SysParameterTable.C_PARAMETERID + "=@" + SysParameterTable.C_PARAMETERID;
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    dpc.AddWithValue("@" + SysParameterTable.C_PARAMETERNAME, entity.ParameterName);
                    dpc.AddWithValue("@" + SysParameterTable.C_MODIFYDATE, entity.ModifyDate);
                    dpc.AddWithValue("@" + SysParameterTable.C_MODIFYUSERID, entity.ModifyUserID);
                    dpc.AddWithValue("@" + SysParameterTable.C_ORDERNUMBER, entity.OrderNumber);
                    dpc.AddWithValue("@" + SysParameterTable.C_DESCRIPTION, entity.Description);
                    dpc.AddWithValue("@" + SysParameterTable.C_PARATMETERVALUE, entity.ParatmeterValue);
                    dpc.AddWithValue("@" + SysParameterTable.C_PARAMETERID, entity.ParameterID);
                    dpc.AddWithValue("@" + SysParameterTable.C_STATUS, entity.Status);
                    return broker.ExecuteSQL(strSQL, dpc);
                    #endregion
            }
        }

        public static int UpdateStatus(int id, Common.Enum.BaseEnum.CommonSatus status)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                #region SQL Server
                    //string strSQL = "update " + SysParameterTable.C_TABLENAME + " set ";
                    //strSQL += SysParameterTable.C_STATUS + "=@" + SysParameterTable.C_STATUS;
                    //strSQL += "," + SysParameterTable.C_MODIFYDATE + "=getdate()"; //Add by alex 20091119
                    //strSQL += " where " + SysParameterTable.C_PARAMETERID + "=@" + SysParameterTable.C_PARAMETERID;
                    //DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    //dpc.AddWithValue("@" + SysParameterTable.C_PARAMETERID, id);
                    //dpc.AddWithValue("@" + SysParameterTable.C_STATUS, ((int)status).ToString());

                string strSQL = "delete from " + SysParameterTable.C_TABLENAME;
                strSQL += " where " + SysParameterTable.C_PARAMETERID + "=@" + SysParameterTable.C_PARAMETERID;
                DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                dpc.AddWithValue("@" + SysParameterTable.C_PARAMETERID, id);
                    return broker.ExecuteSQL(strSQL, dpc);
                    #endregion
            }
        }

        public static int UpdateStatus(int id, Common.Enum.BaseEnum.CommonSatus status, DataAccessBroker broker)
        {
            #region SQL Server
                string strSQL = "update " + SysParameterTable.C_TABLENAME + " set ";
                strSQL += SysParameterTable.C_STATUS + "=@" + SysParameterTable.C_STATUS;
                strSQL += ","+SysParameterTable.C_MODIFYDATE + "=getdate()"; //Add by alex 20091119
                strSQL += " where " + SysParameterTable.C_PARAMETERID + "=@" + SysParameterTable.C_PARAMETERID;
                DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                dpc.AddWithValue("@" + SysParameterTable.C_PARAMETERID, id);
                dpc.AddWithValue("@" + SysParameterTable.C_STATUS, ((int)status).ToString());
                return broker.ExecuteSQL(strSQL, dpc);
                #endregion
        }


        public static DataSet GetAll()
        {
            string strSQL = @"select t.*,ty.parametertypename,u.username as CreatorName,p.username as ModifyUserName from "
                + SysParameterTable.C_TABLENAME + " t  join "
                + SysParametertypeTable.C_TABLENAME + " ty on ty."
                + SysParametertypeTable.C_PARAMETERTYPEID + "=t."
                + SysParameterTable.C_PARAMETERTYPEID + " left join "
                + SysUserTable.C_TABLENAME + " u on t." + SysParametertypeTable.C_CREATEUSERID + "=u."
                + SysUserTable.C_USERID + " left join " + SysUserTable.C_TABLENAME + " p on p."
                + SysUserTable.C_USERID + "=t." + SysParametertypeTable.C_CREATEUSERID;
            strSQL += " order by t." + SysParameterTable.C_PARAMETERID + ",t." + SysParameterTable.C_ORDERNUMBER;
            return OneSoft.DataAccess.DataAccessCommon.SelectDataSet(strSQL);
        }


        public static DataSet GetParamsByTypeID(string paramTypeID)
        {
            string strSql = "SELECT sp.ParameterCode ,sp.ParameterName FROM SYS_Parameter sp ,SYS_ParameterType spt WHERE sp.ParameterTypeID=spt.ParameterTypeID AND spt.ParameterTypeID='IndustryCategory' ORDER BY sp.OrderNumber";
            return DataAccessCommon.SelectDataSet(strSql);
        }


        #region Sync
        public static DataSet GetParamAndParamType(DateTime lastDownLoad)
        {
            DataTable dtbParam = null;

            #region SQL Server
                DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                String ModifyTime = String.Empty;
                //DataTable dtbParamType = null;
                if (DateTime.Compare(lastDownLoad, DateTime.MinValue) != 0)
                {
                    ModifyTime = String.Format(" AND ({0} IS NULL OR {0} >@LastSyncTime )", SysParametertypeTable.C_MODIFYDATE);
                    dpc.AddWithValue("@LastSyncTime", lastDownLoad);
                    //dtbParamType = DataAccessCommon.Select(String.Format("SELECT * FROM SYS_ParameterType WHERE 1=1 {0} ", ModifyTime), dpc).Tables[0];
                    dtbParam = DataAccessCommon.Select(String.Format("SELECT * FROM SYS_Parameter WHERE 1=1 {0} ", ModifyTime), dpc).Tables[0];

                }
                else
                {
                    //dtbParamType = Suzsoft.DataAccess.DataAccessCommon.SelectDataSet("SELECT * FROM SYS_ParameterType").Tables[0];
                    dtbParam = OneSoft.DataAccess.DataAccessCommon.SelectDataSet("SELECT * FROM SYS_Parameter").Tables[0];
                }
                #endregion

            DataSet ds = new DataSet();

            if (dtbParam.Rows.Count > 0)
            {
                dtbParam.TableName = SysParameterTable.C_TABLENAME;
                ds.Tables.Add(dtbParam.Copy());
            }
            return ds;
        }
        #endregion
    }
}
